package com.android.model.info;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;

/**
 * Created by TenFace
 * on 2017/7/18.
 */

public class LocalSp {
    /**
     * int long 等整型的默认取值
     */
    public static final int DEFAULT_VALUE = 0;

    private static final String DB_NAME = "userDb";
    private static final String NO_CLEAR_DB_NAME = "noClearDbName";

    private String KEY_SSO_TOKEN = "KEY_SSO_TOKEN";
    private String KEY_SSO_TRADE_TOKEN = "KEY_SSO_TRADE_TOKEN";
    private String KEY_SSO_TRADE_EXPIRED_TIME = "KEY_SSO_TRADE_EXPIRED_TIME";
    private String KEY_IS_SETTING_TRADE_PASSWORD = "KEY_IS_SETTING_TRADE_PASSWORD";//查询是否设置过交易密码
    private String KEY_ACCOUNT_INFO_ID = "KEY_ACCOUNT_INFO_ID";
    private String KEY_ACCOUNT_INFO_STATUS = "KEY_ACCOUNT_INFO_STATUS";
    private String KEY_ACCOUNT_TOKEN = "KEY_ACCOUNT_TOKEN";
    private String KEY_ORDER_TRADE_TOKEN = "KEY_ORDER_TRADE_TOKEN";
    private String KEY_ORDER_TRADE_EXPIRED_TIME = "KEY_ORDER_TRADE_EXPIRED_TIME";
    private String KEY_ORDER_TRADE_SESSION = "KEY_ORDER_TRADE_SESSION";
    private String KEY_ORDER_TRADE_TICKET = "KEY_ORDER_TRADE_TICKET";
    private String _KEY_SESSION_EXPIRED_TIME = "_KEY_SESSION_EXPIRED_TIME";//session过期时间


    private String KEY_UPS_DOWNS_COLOR_CHECKED_STATUS = "KEY_UPS_DOWNS_COLOR_CHECKED_STATUS";//CheckBox选中的状态
    private String KEY_APP_BEGIN_COLOR = "KEY_APP_BEGIN_COLOR";//开始的颜色
    private String KEY_APP_END_COLOR = "KEY_APP_END_COLOR";//结束的颜色
    private String KEY_APP_DEFAULT_COLOR = "KEY_APP_DEFAULT_COLOR";//默认的颜色
    private String KEY_UPS_LANGUAGE_STATUS = "KEY_UPS_LANGUAGE_STATUS";//语言

    private int redColor = Color.argb(255, 220, 56, 44);//红
    private int greenColor = Color.argb(255, 39, 167, 129);//绿
    private int defaultColor = Color.argb(255, 170, 170, 170);//灰色

    private static final String OPEN_ACCOUNT_INVESTED_INFO = "OPEN_ACCOUNT_INVESTED_INFO";
    private static volatile LocalSp instance;

    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    private SharedPreferences noClearSp;
    private SharedPreferences.Editor noClearEditor;

    @SuppressLint("CommitPrefEdits")
    private LocalSp(Context context) {
        sp = context.getApplicationContext().getSharedPreferences(DB_NAME, Context.MODE_PRIVATE);
        editor = sp.edit();
        noClearSp = context.getApplicationContext().getSharedPreferences(NO_CLEAR_DB_NAME, Context.MODE_PRIVATE);
        noClearEditor = noClearSp.edit();
    }

    public static LocalSp getInstance(Context context) {
        if (instance == null) {
            synchronized (LocalSp.class) {
                if (instance == null) {
                    instance = new LocalSp(context);
                }
            }
        }
        return instance;
    }

    /**
     * 清理sp 中的数据
     */
    public void clearDb() {
        editor.clear();
        editor.commit();
    }


    /**
     * 保存用户是否设置过交易密码
     *
     * @param isSettingTradePassword isSettingTradePassword
     */
    public void saveIsSettingTradePassword(boolean isSettingTradePassword) {
        editor.putBoolean(KEY_IS_SETTING_TRADE_PASSWORD, isSettingTradePassword);
        editor.commit();
    }

    /**
     * 获取是否设置了交易密码
     *
     * @return 获取是否设置了交易密码
     */
    public boolean getIsSettingTradePassword() {
        return sp.getBoolean(KEY_IS_SETTING_TRADE_PASSWORD, false);
    }

    public void saveSSOToken(String ssoToken) {
        editor.putString(KEY_SSO_TOKEN, ssoToken);
        editor.commit();
    }

    public String getSSOToken() {
        return sp.getString(KEY_SSO_TOKEN, null);
    }

    public void saveSSOTradeToken(String ssoTradeToken) {
        editor.putString(KEY_SSO_TRADE_TOKEN, ssoTradeToken);
        editor.commit();
    }

    public String getSSOTradeToken() {
        return sp.getString(KEY_SSO_TRADE_TOKEN, null);
    }

    /**
     * 保存交易token 过期时间
     *
     * @param expiredTime 过期时间
     */
    public void saveSSOTradeExpiredTime(long expiredTime) {
        editor.putLong(KEY_SSO_TRADE_EXPIRED_TIME, expiredTime);
        editor.commit();
    }

    /**
     * 获取交易token过期时间
     *
     * @return 交易token过期时间
     */
    public long getSSOTradeTokenExpiredTime() {
        return sp.getLong(KEY_SSO_TRADE_EXPIRED_TIME, DEFAULT_VALUE);
    }

    public void saveAccountInfoId(String accountInfoId) {
        editor.putString(KEY_ACCOUNT_INFO_ID, accountInfoId);
        editor.commit();
    }

    public String getAccountInfoId() {
        return sp.getString(KEY_ACCOUNT_INFO_ID, null);
    }

    public void saveAccountInfoStatus(String accountInfoStatus) {
        editor.putString(KEY_ACCOUNT_INFO_STATUS, accountInfoStatus);
        editor.commit();
    }

    public String getAccountInfoStatus() {
        return sp.getString(KEY_ACCOUNT_INFO_STATUS, null);
    }

    public void saveAccountToken(String accountToken) {
        editor.putString(KEY_ACCOUNT_TOKEN, accountToken);
        editor.commit();
    }

    public String getAccountToken() {
        return sp.getString(KEY_ACCOUNT_TOKEN, null);
    }

    public void saveOrderTradeToken(String orderTradeToken) {
        editor.putString(KEY_ORDER_TRADE_TOKEN, orderTradeToken);
        editor.commit();
    }

    public String getOrderTradeToken() {
        return sp.getString(KEY_ORDER_TRADE_TOKEN, null);
    }

    public void saveOrderTradeTokenExpiredTime(long orderTradeToken) {
        editor.putLong(KEY_ORDER_TRADE_EXPIRED_TIME, orderTradeToken);
        editor.commit();
    }

    public long getOrderTradeTokenExpiredTime() {
        return sp.getLong(KEY_ORDER_TRADE_EXPIRED_TIME, DEFAULT_VALUE);
    }

    public void saveOrderTradeSession(String orderTradeSession, String sessionType) {
        editor.putString(sessionType + KEY_ORDER_TRADE_SESSION, orderTradeSession);
        editor.commit();
    }

    public String getOrderTradeSession(String sessionType) {
        return sp.getString(sessionType + KEY_ORDER_TRADE_SESSION, null);
    }

    public void saveOrderTradeTicket(String orderTradeTicket) {
        editor.putString(KEY_ORDER_TRADE_TICKET, orderTradeTicket);
        editor.commit();
    }

    /**
     * 保存session过期时间
     *
     * @param sessionType        session类型
     * @param sessionExpiredTime 过期时间
     */
    public void saveSessionExpiredTime(String sessionType, long sessionExpiredTime) {
        editor.putLong(sessionType + _KEY_SESSION_EXPIRED_TIME, sessionExpiredTime);
        editor.commit();
    }

    /**
     * 获取 sessionType 过期时间
     *
     * @param sessionType session类型
     * @return session过期时间
     */
    public long getSessionExpiredTime(String sessionType) {
        return sp.getLong(sessionType + _KEY_SESSION_EXPIRED_TIME, DEFAULT_VALUE);
    }

    public String getOrderTradeTicket() {
        return sp.getString(KEY_ORDER_TRADE_TICKET, null);
    }


    /*-------------------------------------------K线相关的缓存START------------------------------------------*/


    /**
     * 保存day
     *
     * @param day day
     */
    public void saveDay(String day) {
        editor.putString("QUOTE_KLINE_DATA_DAY", day);
        editor.commit();
    }

    /**
     * 获取day
     *
     * @return day
     */
    public String getDay() {
        return sp.getString("QUOTE_KLINE_DATA_DAY", null);
    }

    /**
     * remove day
     */
    public void clearDay() {
        editor.remove("QUOTE_KLINE_DATA_DAY");
        editor.commit();
    }

    /**
     * week
     *
     * @param week week
     */
    public void saveWeek(String week) {
        editor.putString("QUOTE_KLINE_DATA_WEEK", week);
        editor.commit();
    }

    /**
     * week
     *
     * @return week
     */
    public String getWeek() {
        return sp.getString("QUOTE_KLINE_DATA_WEEK", null);
    }

    /**
     * remove day
     */
    public void clearWeek() {
        editor.remove("QUOTE_KLINE_DATA_WEEK");
        editor.commit();
    }

    /**
     * month
     *
     * @param month month
     */
    public void saveMonth(String month) {
        editor.putString("QUOTE_KLINE_DATA_MONTH", month);
        editor.commit();
    }

    /**
     * 获取month
     *
     * @return month
     */
    public String getMonth() {
        return sp.getString("QUOTE_KLINE_DATA_MONTH", null);
    }

    /**
     * remove day
     */
    public void clearMonth() {
        editor.remove("QUOTE_KLINE_DATA_MONTH");
        editor.commit();
    }

    /*-------------------------------------------K线相关的缓存END------------------------------------------*/


    /*----------------------永远不清理的sharedPreference 直到手动清理或卸载应用---------------------*/


    public void saveBeginColor(int color) {
        noClearEditor.putInt(KEY_APP_BEGIN_COLOR, color);
        noClearEditor.commit();
    }

    public int getBeginColor() {
        return noClearSp.getInt(KEY_APP_BEGIN_COLOR, greenColor);
    }

    public void saveEndColor(int color) {
        noClearEditor.putInt(KEY_APP_END_COLOR, color);
        noClearEditor.commit();
    }

    public int getEndColor() {
        return noClearSp.getInt(KEY_APP_END_COLOR, redColor);
    }

    public void saveDefaultColor(int color) {
        noClearEditor.putInt(KEY_APP_DEFAULT_COLOR, color);
        noClearEditor.commit();
    }

    public int getDefaultColor() {
        return noClearSp.getInt(KEY_APP_DEFAULT_COLOR, defaultColor);
    }

    public void saveCheckColorStatus(int count) {
        noClearEditor.putInt(KEY_UPS_DOWNS_COLOR_CHECKED_STATUS, count);
        noClearEditor.commit();
    }

    public int getCheckColorStatus() {
        return noClearSp.getInt(KEY_UPS_DOWNS_COLOR_CHECKED_STATUS, 0);
    }

    public void saveLanguageStatus(String language) {
        noClearEditor.putString(KEY_UPS_LANGUAGE_STATUS, language);
        noClearEditor.commit();
    }

    public String getLanguageStatus() {
        return noClearSp.getString(KEY_UPS_LANGUAGE_STATUS, "en");
    }

}
